package com.example.shixunandjavagotoyangchen.BoosYang.Day003;

import android.util.Log;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.viewpager.widget.ViewPager;

import com.example.modelviewpresenter.View.BasterFragment;
import com.example.shixunandjavagotoyangchen.BoosYang.Day003.Day003_FX2.BlankFragmentCommunity1;
import com.example.shixunandjavagotoyangchen.BoosYang.Day003.Day003_FX2.BlankFragmentCommunity2;
import com.example.shixunandjavagotoyangchen.BoosYang.Day003.Day003_FX2.CommunityAdpter;
import com.example.shixunandjavagotoyangchen.BoosYang.Day003.Day003_FX2.CommunityBean;
import com.example.shixunandjavagotoyangchen.BoosYang.Day003.Day003_FX2.CommunityContion;
import com.example.shixunandjavagotoyangchen.BoosYang.Day003.Day003_FX2.CommunityModel;
import com.example.shixunandjavagotoyangchen.BoosYang.Day003.Day003_FX2.CommunityPresenter;
import com.example.shixunandjavagotoyangchen.R;
import com.example.shixunandjavagotoyangchen.Sunshine.FragmentSpaek.FragmentSpaek1;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

public class BlankFragmentDay003_FX2 extends BasterFragment implements CommunityContion.ofviss, OnRefreshLoadMoreListener {

    private ViewPager CommunityViewPager;
    private ImageView CommunityImageView1;
    private ImageView CommunityImageView2;
    private RecyclerView CommunityRecyclerView;
    public boolean iscommunity = true;
    public int communityPager = 33;
    public CommunityPresenter presenter;
    public CommunityAdpter adpter;
    private SmartRefreshLayout CommunitySmartRefreshLayout;

    @Override
    public int BandLayout() {
        return R.layout.fragment_blank_fragment_day003__fx2;
    }

    @Override
    public void initView() {
        CommunitySmartRefreshLayout = (SmartRefreshLayout) findViewById(R.id.Community_SmartRefreshLayout);
        CommunityViewPager = (ViewPager) findViewById(R.id.Community_ViewPager);
        CommunityImageView1 = (ImageView) findViewById(R.id.Community_ImageView1);
        CommunityImageView2 = (ImageView) findViewById(R.id.Community_ImageView2);
        CommunityRecyclerView = (RecyclerView) findViewById(R.id.Community_RecyclerView);
        CommunityRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        CommunitySmartRefreshLayout.setOnRefreshLoadMoreListener(this);
     presenter =new     CommunityPresenter(this,new CommunityModel());
        presenter.communitying("/video/findVideos?currentPage="+communityPager+"&pageSize=20");

    }

    @Override
    public void initDate() {

        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new BlankFragmentCommunity1());
        fragments.add(new BlankFragmentCommunity2());

        FragmentSpaek1 spaek1 = new FragmentSpaek1(getActivity().getSupportFragmentManager(), FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, fragments);
        CommunityViewPager.setAdapter(spaek1);


        CommunityViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        CommunityImageView1.setImageResource(R.drawable.xiaodian_ture);
                        CommunityImageView2.setImageResource(R.drawable.xiaodian_flass);
                        break;

                    case 1:
                        CommunityImageView1.setImageResource(R.drawable.xiaodian_flass);
                        CommunityImageView2.setImageResource(R.drawable.xiaodian_ture);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    public void getofviss(List<CommunityBean.DataBean> dataBeans) {
        CommunitySmartRefreshLayout.finishLoadMore();
        CommunitySmartRefreshLayout.finishRefresh();
        if (adpter==null){
            adpter=new CommunityAdpter(dataBeans);
            CommunityRecyclerView.setAdapter(adpter);
        }else {
            if (iscommunity){
                adpter.getData().clear();
            }
            adpter.getData().addAll(dataBeans);
            adpter.notifyDataSetChanged();
        }

    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        iscommunity =false;
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {

        if (iscommunity){
            Log.i("aaa","la1");
        }
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();

    }
}
